var pressEnter = false;
var clickCalendar = false;
var isBetweenDate = false;
var currentField = 0;

function checkKeyDate(obj, curField) {
	var returnValue = false;
	clickCalendar = false;
	currentField = ((curField == undefined || curField == "") ? 0  : curField);
// Only Common-key, "-", ".", "/" Backspace and 0-9 Key-in Textfield
	if (isCommonKey()) {
		returnValue =  true;
	} else {
		if (event.keyCode==8 || event.keyCode==37 || event.keyCode==39 || (event.keyCode<=57 && event.keyCode>=48) || (event.keyCode<=105 && event.keyCode>=96) || (event.keyCode<=111 && event.keyCode>=109) || (event.keyCode<=191 && event.keyCode>=189)) {
			returnValue =  true;
		}
	}
	if (event.keyCode==13) {
//alert(event.srcElement.tagName);
		pressEnter = true;
		return validateDate(obj);
	}
	return returnValue;
}

function checkValidDate(obj, betweenDate) {
	isBetweenDate = ((betweenDate == undefined) ? true  : betweenDate);
	if (!(pressEnter || clickCalendar)) {
		return validateDate(obj)
	} else {
		return false;
	}
}

function validateDate(obj) {
	srcValue = obj.value;
	var returnValue = false;
	dateString = "";
	dateStringArray = new Array();
	title = "Please check date value";
	msg = "";
	okBtnLabel = "OK";
	century = "20";
	minYear = 0;
	maxYear = 9999;
	minMonth = 1;
	maxMonth = 12;
	minDay = 1;
	maxDay = 31;
	minThaiYear = 2400;
	maxThaiYear = 2700;

	if (srcValue == "") {
		returnValue = true;
	}

	if (srcValue.indexOf("/") == -1 && srcValue.indexOf("-") == -1 && srcValue.indexOf(".") == -1) {
		if (srcValue.length == 6 || srcValue.length == 8) {
			dateStringArray[0] = srcValue.substring(0, 2);
			dateStringArray[1] = srcValue.substring(2, 4);
			dateStringArray[2] = (srcValue.length == 6 ? century+srcValue.substring(4, 6) : srcValue.substring(4, 8));
		} else {
			msg = "Not valid date format";
		}
	}

	if (srcValue.split("/").length == 3) {
		dateStringArray = srcValue.split("/");
	} else if (srcValue.split("-").length == 3) {
		dateStringArray = srcValue.split("-");
	} else if (srcValue.split(".").length == 3) {
		dateStringArray = srcValue.split(".");
	}

	if (dateStringArray.length == 3) {
		dateStringArray[2] = (dateStringArray[2].length == 2 ? century+dateStringArray[2] : dateStringArray[2]);
		if (parseInt(parseFloat(dateStringArray[0])) != dateStringArray[0]) {
			msg = "Not valid day. Please insert number only";
		} else if (parseInt(parseFloat(dateStringArray[1])) != dateStringArray[1]) {
			msg = "Not valid month. Please insert number only";
		} else if (parseInt(parseFloat(dateStringArray[2])) != dateStringArray[2]) {
			msg = "Not valid year. Please insert number only";
		} else {
			if (!(parseInt(parseFloat(dateStringArray[2])) >= minYear && parseInt(parseFloat(dateStringArray[2])) <= maxYear)) {
				msg = "Not valid year. Year should between "+minYear+" and "+maxYear;
			} else if (!(parseInt(parseFloat(dateStringArray[1])) >= minMonth && parseInt(parseFloat(dateStringArray[1])) <= maxMonth)) {
				msg = "Not valid month. Month should between "+minMonth+" and "+maxMonth;
			} else {
				maxDay = findDayInMonthYear(parseInt(parseFloat(dateStringArray[1])), parseInt(parseFloat(dateStringArray[2])));
				if (!(parseInt(parseFloat(dateStringArray[0])) >= minDay && parseInt(parseFloat(dateStringArray[0])) <= maxDay)) {
					msg = "Not valid day. Day should between "+minDay+" and "+maxDay;
				} else {
					dateStringArray[0] = (dateStringArray[0].length == 1 ? "0"+dateStringArray[0] : dateStringArray[0]);
					dateStringArray[1] = (dateStringArray[1].length == 1 ? "0"+dateStringArray[1] : dateStringArray[1]);
					dateStringArray[2] = (dateStringArray[2].length == 1 ? "000"+dateStringArray[2] : (dateStringArray[2].length == 3 ? "0"+dateStringArray[2] : dateStringArray[2]));
					obj.value = dateStringArray[0]+"/"+dateStringArray[1]+"/"+dateStringArray[2];
					returnValue = true;
				}
			}
		}
	} else {
		msg = "Not valid date format";
	}
	if (!returnValue) {
//alert(msg);
		showOWarningDialog(title, msg, okBtnLabel);
		obj.focus();
	} else if (isBetweenDate) {
		if (obj.parentElement.name == undefined) {
			returnValue = checkBetweenDate(eval("document.all."+obj.name));
		} else {
			returnValue = checkBetweenDate(eval(obj.parentElement.name+"."+obj.name));
		}
	}
	pressEnter = false;
	clickCalendar = false;
	return returnValue;
}

function findDayInMonthYear(month, year) {
	var returnValue = 31;
	if (year>=minThaiYear && year<=maxThaiYear) {
		year = year-543;
	}
	dayInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	if (year%4 == 0 && month == 2) {
		dayInMonth[1] = 29;
	}
	returnValue = dayInMonth[month-1];
	return returnValue;
}

function showCalendar(returnToObj) {
	dateLayer.style.visibility = 'visible';
}

function hideLayer(layer) {
	layer.style.visibility = 'hidden';
}

//function returnDate(objName,objLayer,objID) {
	function returnDate(objLayer,objID) {
	if (event.srcElement.className.substr(0,6)=="Day_ms" || event.srcElement.className.substr(0,6)=="DaySel") {
		hideLayer(objLayer);
		if (eval(currentField).value == undefined) {
//			functionStr = "assignValue("+objName+"["+currentField+"],"+objID.id+")";
			functionStr = "assignValue("+currentField+","+objID.id+")";
			setTimeout(functionStr,50);
			setTimeout("checkBetweenDateFromCalendar("+currentField+")",100);
		} else {
			functionStr = "assignValue("+currentField+","+objID.id+")";
			setTimeout(functionStr,50);
		}
	}
}

function assignValue(obj,objID) {
	var cDay = ""+objID.day;
	var cMonth = ""+objID.month;
	var cYear = ""+objID.year;
//	if (cDay.length == 1) cDay = "0"+cDay;
//	if (cMonth.length == 1) cMonth = "0"+cMonth; 
	obj.value = cDay+'/'+cMonth+'/'+cYear;
	obj.focus();
}

function checkBetweenDateFromCalendar(objName) {
	clickCalendar = true;
//	checkBetweenDate(objName);
	clickCalendar = checkBetweenDate(objName);
}

function checkBetweenDate(objName) {
	var returnValue = true;
//alert("event.srcElement.value = "+event.srcElement.value);
//alert("objName[0].value = "+objName[0].value);
//alert("currentField = "+currentField);
	if (objName[0].value != "" && objName[1].value != "") {
		if (compareValue("date",objName[0], objName[1], "<=", "<B>From date</B>", "<B>To date</B>", currentField+1)) {
//				dateLayer.style.visibility='hidden';
		} else {
			if (currentField == 0) {
				objName[0].value = objName[1].value;
			} else {
				objName[1].value = objName[0].value;
			}
			returnValue = false;
		}
	}
	hideLayer(objName);
//alert("returnValue = "+returnValue);
	return returnValue;
}

function showLayer(numField,objName) {
//	alert('document.all.'+numField.name+"------"+objName.id);
	currentField = 'document.all.'+numField.name;
	posX = 0;
	posY = 0;
	posX = ((document.body.clientWidth - window.event.x) > objName.clientWidth) ? window.event.x : window.event.x - objName.clientWidth ;
	posX = (document.body.clientWidth < objName.clientWidth) ? window.event.x : posX;
	posY = ((document.body.clientHeight - window.event.y) > objName.clientHeight) ? window.event.y : window.event.y - objName.clientHeight ;
	posY = (document.body.clientHeight < objName.clientHeight) ? window.event.y : posY;
/*
alert("window width = "+document.body.clientWidth);
alert("window height = "+document.body.clientHeight);
alert("objName.style.width = "+objName.clientWidth);
alert("objName.style.height = "+objName.clientHeight);
*/
	objName.style.left = document.body.scrollLeft+posX;
	objName.style.top =document.body.scrollTop+ posY;
	objName.style.visibility = 'visible';
}

function focusField(numField) {
	currentField = numField;
}

////////////-------------- check min and max year between textbox and calendar when change date---------------\\\\\\\\\\\\
//			checkValidYear(c,obj).
//			c is calender Layer name
//			obj is return object(textbox name)
	function checkValidYear(c,obj)
	{
		var calendar = eval('calendar'+c);					
		// ------- 'calendar'+c is calendar componet ID.
		// ------- if you want to change calendar component ID please 
		// ------- change calendar component ID in calendar generator at method getTagGenerator() too.
		var objLength = obj.value.length; var splitValue	= "/"; var dateArray = obj.value.split(splitValue);
		var year = dateArray[2]; 
		var tempMinYear = calendar.minYear; var tempMaxYear = calendar.maxYear; var lang = calendar.setLanguage ;
		if(lang == 'THAI' ){ tempMinYear = tempMinYear+543; tempMaxYear = tempMaxYear+543 ; }
		if( obj.value!="" &&( Number(year) < tempMinYear || Number(year) > tempMaxYear) ) {
			showOWarningDialog("Data not complete", "Year must between "+tempMinYear+" and "+tempMaxYear+".", "OK") ;
			if(Number(year) < tempMinYear) {
				obj.value = dateArray[0]+splitValue+dateArray[1]+splitValue+ tempMinYear;
			}else{
				obj.value =  dateArray[0]+splitValue+dateArray[1]+splitValue+ tempMaxYear;
			}
			obj.focus();
		}
	}
////////////-------------- set calendar selectiing when change date ---------------\\\\\\\\\\\\
	function setDateToCalendar(obj,objID) 
	{
		var splitValue	= "/";
		var dateArray = obj.value.split(splitValue);
		if( obj.value!="" )
		{
			objID.day	= Number( dateArray[0] ) ;
			objID.month = Number( dateArray[1] ) ;
			dateArray[2]  = Number( dateArray[2] ) > 2500 ? Number( dateArray[2] ) - 543 : Number( dateArray[2] ) ;
			objID.year = Number( dateArray[2] ) ;

		}
	}
